# -*- coding=utf8 -*-
__author__ = 'nickleo'

import shapeview,topicview,shape_utils,progress_utils
from django.http import HttpResponse, HttpResponseRedirect
import jsonpickle as json


def shape_all(request):
    return shapeview.all(request)

def topic_all(request):
    return topicview.all(request)

def cluster_bid_mapping(request):
    list,alert =shape_utils.cluster_bid_mapping()
    return HttpResponse(json.encode(list))

def chart_area(request, shape_id):
    #//"name_server": name_server, "clustername": clustername,"time_type":time_type,"time_time":time_time
    errCode=200
    errDesc=None
    name_server = None
    if request.GET.get('name_server') is None:
        name_server = 'Default name_server'
    else:
        name_server = request.GET.get('name_server')

    query_time = None
    if request.GET.get('time_type') is None:
        errDesc ="time_type  is None "
        errCode=400;
    else:
        query_time = request.GET.get('query_time')


    clustername = None
    if request.GET.get('clustername') is None:
        errCode=400
        errDesc='clustername is None'
    else:
        clustername = request.GET.get('clustername')


    time_type = None
    if request.GET.get('time_type') is None:
        errCode=400
        errDesc='time_type is None'
    else:
       time_type = request.GET.get('time_type')

    time_time = None
    if request.GET.get('time_time') is None:
        errCode=400
        errDesc='time_time is None'
    else:
        time_time = request.GET.get('time_time')

    datatype = None
    if request.GET.get('datatype') is None:
        errCode=400
        errDesc='datatype is None'
    else:
        datatype = request.GET.get('datatype')

    if(errCode !=200) :
        resp = HttpResponse()
        resp.status_code=errCode
        resp.reason_phrase=errDesc
        return resp
    else:
        result,action_alert = shape_utils.shapeAreaCharts(clustername,shape_id,time_type,time_time,name_server,datatype)

        return HttpResponse(json.dumps(result))

def get_progress_diff_data(request):
    errCode=200
    errDesc=None
    name_server = None
    if request.GET.get('name_server') is None:
        name_server = None
    else:
        name_server = request.GET.get('name_server')

    group = None
    if request.GET.get('group') is None:
        errDesc ="group  is None "
        errCode=400;
    else:
        group = request.GET.get('group')
    result,action_alert = progress_utils.get_progress_data(name_server,group)
    return HttpResponse(json.dumps(result))
